﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Data;

namespace AnyOffice.DAL
{
    public class MapInfoDao
    {
        public bool Delete(SqlConnection conn, int bookId)
        {
            string commandText = "DELETE FROM Map WHERE [MapId] = @MapId ";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MapId", SqlDbType.Int) };
            commandParameters[0].Value = bookId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }


        public int Insert(SqlConnection conn, MapData data)
        {
            string sql = @"INSERT INTO Map
                           (Longitude
                           ,Latitude
                           ,Name
                           ,Address
                           ,MapType)
                            VALUES(@Longitude
                           ,@Latitude
                           ,@Name
                           ,@Address
                           ,@MapType) SELECT SCOPE_IDENTITY() AS RetValue";

            SqlParameter[] commandParameters = new SqlParameter[5];

            commandParameters[0] = new SqlParameter("@Longitude", SqlDbType.Decimal);
            commandParameters[0].Value = data.Longitude;

            commandParameters[1] = new SqlParameter("@Latitude", SqlDbType.Decimal);
            commandParameters[1].Value = data.Latitude;


            commandParameters[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 200);
            commandParameters[2].Value = data.Name;

            commandParameters[3] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            commandParameters[3].Value = data.Address;


            commandParameters[4] = new SqlParameter("@MapType", SqlDbType.Int);
            commandParameters[4].Value = data.MapType;

            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, commandParameters);
            if (obj2 != DBNull.Value)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }
        public bool Update(SqlConnection conn, MapData data)
        {
            string commandText = @"UPDATE Map
                           SET Longitude =@Longitude
                          ,Latitude = @Latitude
                          ,[Name] = @Name
                          ,[Address] = @Address
                          ,MapType = @MapType WHERE MapID=@MapID";

            SqlParameter[] commandParameters = new SqlParameter[6];
            commandParameters[0] = new SqlParameter("@Longitude", SqlDbType.Decimal);
            commandParameters[0].Value = data.Longitude;

            commandParameters[1] = new SqlParameter("@Latitude", SqlDbType.Decimal);
            commandParameters[1].Value = data.Latitude;


            commandParameters[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 200);
            commandParameters[2].Value = data.Name;

            commandParameters[3] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            commandParameters[3].Value = data.Address;


            commandParameters[4] = new SqlParameter("@MapType", SqlDbType.Int);
            commandParameters[4].Value = data.MapType;

            commandParameters[5] = new SqlParameter("@MapID", SqlDbType.Int);
            commandParameters[5].Value = data.MapId;

            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public DataTable SelectByMapType(SqlConnection conn, int MapType)
        {
            string commandText = @"SELECT MapID,Longitude
                           ,Latitude
                           ,Name
                           ,Address
                           ,MapType FROM Map WHERE MapType=@MapType";


            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MapType", SqlDbType.Int) };
            commandParameters[0].Value = MapType;

            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Map";
            return set.Tables[0];
        }
    }
}
